<?php
	require("libraries/student.php");
	class Tours extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_tours");
			$this->load->library("string");
			$this->load->helper("form");
			$this->load->library("form_validation");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/tours/index/';
			$config['total_rows'] = $this->model_tours->count_all();
			$data['title'] = "Manage tours";
			$data['template'] = "tours/list_tour";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 4;
			$data['listall']= $this->model_tours->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"tour_status" => $status 
			);
			$this->model_tours->update_status($db,$id);
			redirect(base_url()."admin/tours/");
		}
		public function getcago(){
			$cate_id = $this->input->post("val");
			$listall = $this->model_tours->listcago($cate_id);
			echo '<select name="cago_id" id="cago_id">';
	            echo '<option value="0">Please select</option>';         	
			foreach($listall as $items){
				echo "<option value='".$items['cago_id']."'>".$items['cago_name']."</option>";
			}
			echo "</select>";
		}
		public function add(){
		  	$data['title'] 		= "Add tour";
			$data['template'] 	= "tours/add_tour";
			$data['albums'] 	= $this->model_tours->albums();
			$data['data']['category'] = $this->model_tours->listcate();
			$data['act'] = 4;
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('tour_name'))));
					$db = array(
						"tour_name" => $this->input->post("tour_name"),
						"tour_name_rewrite" => $frienly,
						"tour_code" => $this->input->post("tour_code"),
						"tour_type" => $this->input->post("tour_type"),
						"tour_destina" => $this->input->post("tour_destina"),
						"tour_star"  => $this->input->post("tour_star"),
						"tour_ltinerary" => $this->input->post("tour_ltinerary"),
						"tour_fact" => $this->input->post("tour_fact"),		
						"tour_order" => $this->input->post("tour_order"),
						"tour_price" => $this->input->post("tour_price"),
						"tour_price_old" => $this->input->post("tour_price_old"),
						"tour_show"   => $this->input->post("tour_show"),
						"tour_short_info" => $this->input->post("tour_short_info"),
                        "tour_info"  => $this->input->post("tour_info"),
                        "tour_include"  => $this->input->post("tour_include"),
						"tour_promo_title" => $this->input->post("tour_promo_title"),
						"tour_promo_val" => $this->input->post("tour_promo_val"),
						"tour_map"  => $this->input->post("tour_map"),
						"tour_height" => $this->input->post("tour_height"),
						"tour_status" => "1",
						"cago_id"     => $this->input->post("cago_id"),
						"cate_id"     => $this->input->post("cate_id"),
						"album_id"	  => $this->input->post("tour_album")
					);
					if($_FILES['imgdeal']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("imgdeal")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image_deal'] = $data['file_name'];
							$this->createThumbnail($db['tour_image_deal'],"tours",215,227);
						}
					}
					if($_FILES['tour_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image1'] = $data['file_name'];
							$this->createThumbnail($db['tour_image1'],"tours",400,300);
						}
					}
					if($_FILES['tour_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image2'] = $data['file_name'];
							$this->createThumbnail($db['tour_image2'],"tours",400,300);
						}
					}
					if($_FILES['tour_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image3'] = $data['file_name'];
							$this->createThumbnail($db['tour_image3'],"tours",400,300);
						}
					}
					if($_FILES['tour_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image4'] = $data['file_name'];
							$this->createThumbnail($db['tour_image4'],"tours",400,300);
						}
					}
					$this->model_tours->add($db);
					redirect(base_url()."admin/tours");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title'] 		= "Edit tour";
			$data['template'] 	= "tours/edit_tour";
			$data['albums'] 	= $this->model_tours->albums();
			$data['data'] = "";
			$data['data']['category'] = $this->model_tours->listcate();
			$data['getdata'] = $this->model_tours->getdata($id);
			$data['act'] = 4;
			$data['stt'] = $data['getdata']['cate_id'];
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('tour_name'))));
					$db = array(
						"tour_name" => $this->input->post("tour_name"),
						"tour_name_rewrite" => $frienly,
						"tour_code" => $this->input->post("tour_code"),
						"tour_type" => $this->input->post("tour_type"),
						"tour_destina" => $this->input->post("tour_destina"),
						"tour_star"  => $this->input->post("tour_star"),
						"tour_ltinerary" => $this->input->post("tour_ltinerary"),
						"tour_fact" => $this->input->post("tour_fact"),		
						"tour_order" => $this->input->post("tour_order"),
						"tour_price" => $this->input->post("tour_price"),
						"tour_price_old" => $this->input->post("tour_price_old"),
						"tour_show"   => $this->input->post("tour_show"),
						"tour_short_info" => $this->input->post("tour_short_info"),
                        "tour_info"  => $this->input->post("tour_info"),
                        "tour_include"  => $this->input->post("tour_include"),
						"tour_promo_title" => $this->input->post("tour_promo_title"),
						"tour_promo_val" => $this->input->post("tour_promo_val"),
						"tour_map"  => $this->input->post("tour_map"),
						"tour_height" => $this->input->post("tour_height"),
						"tour_status" => "1",
						"cago_id"     => $this->input->post("cago_id"),
						"cate_id"     => $this->input->post("cate_id"),
						"album_id"	  => $this->input->post("tour_album")
					);
					if($_FILES['imgdeal']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("imgdeal")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add tour";
							$data['template'] 	= "tours/add_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 4;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image_deal'] = $data['file_name'];
							$this->createThumbnail($db['tour_image_deal'],"tours",215,227);
						}
					}
					if($_FILES['tour_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit tour";
							$data['template'] 	= "tours/edit_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['getdata'] = $this->model_tours->getdata($id);
							$data['act'] = 4;
							$data['stt'] = $data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image1'] = $data['file_name'];
							$this->createThumbnail($db['tour_image1'],"tours",400,300);
						}
					}
					if($_FILES['tour_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit tour";
							$data['template'] 	= "tours/edit_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['getdata'] = $this->model_tours->getdata($id);
							$data['act'] = 4;
							$data['stt'] = $data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image2'] = $data['file_name'];
							$this->createThumbnail($db['tour_image2'],"tours",400,300);
						}
					}
					if($_FILES['tour_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit tour";
							$data['template'] 	= "tours/edit_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['getdata'] = $this->model_tours->getdata($id);
							$data['act'] = 4;
							$data['stt'] = $data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image3'] = $data['file_name'];
							$this->createThumbnail($db['tour_image3'],"tours",400,300);
						}
					}
					if($_FILES['tour_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/tours';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("tour_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit tour";
							$data['template'] 	= "tours/edit_tour";
							$data['albums'] 	= $this->model_tours->albums();
							$data['data'] = "";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['getdata'] = $this->model_tours->getdata($id);
							$data['act'] = 4;
							$data['stt'] = $data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['tour_image4'] = $data['file_name'];
							$this->createThumbnail($db['tour_image4'],"tours",400,300);
						}
					}
					$this->model_tours->update($db,$id);
					redirect(base_url()."admin/tours");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_tours->getdata($id);
			@unlink("uploads/tours/logo/".$data['tour_logo']);
			@unlink("uploads/tours/logo/thumb/".$data['tour_logo']);
			$this->model_tours->del($id);
			redirect(base_url()."admin/tours");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}